//
//  ListDetailViewController.m
//  Jlcf_ios
//
//  Created by Ji on 2018/9/27.
//  Copyright © 2018 ski. All rights reserved.
//

#import "zzknojJLCFListDetailViewControllerFwedjf.h"
#import "XvmSEfJLCFListRequestdkZIyb.h"
#import "xvEfznJLCFListDetailBmapCellEkyNlk.h"
#import "IgAVWgJLCFListDetailWebViewControllerOVuLRc.h"
#import "xXxnSVJLCFListDetailCellDMjeNR.h"
#import "fTtJkxJLCFListRecordViewControllerlJnAft.h"
#import "gKEIjmJLCFWillPayViewControllermzyydi.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "CEMCRXJLCFListPreparePayControllerJuykET.h"
@interface zzknojJLCFListDetailViewControllerFwedjf ()

@property (nonatomic, strong) QddEjIJLCFListDetailResultModelNVtUPx *CUWORhdetailModelIRREMd;

@end

@implementation zzknojJLCFListDetailViewControllerFwedjf

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT - 50 - IPHONEX_SPACE);
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
    self.view.backgroundColor = QTZ_Gray237;
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 230 + IPHONEX_SPACE)];
    rRHNlxheaderViewfJQeRl.backgroundColor = Theme_Color;
    self.tableView.tableHeaderView = rRHNlxheaderViewfJQeRl;
    
    
    [self addNavigationBar];
    [self loadData];
    
    UIButton *buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, FULL_HEIGHT - 50 - IPHONEX_SPACE, FULL_WIDTH - 76, 40)];
    [buyBtn setBackgroundColor:Theme_Color];
    [buyBtn setTitle:@"立即出借" forState:0];
    buyBtn.titleLabel.font = FONT(15);
    [buyBtn setTitleColor:QTZ_White forState:0];
    buyBtn.layer.cornerRadius = 5;
    [buyBtn addTarget:self action:@selector(handleBuy) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buyBtn];
    
}

- (void)handleBuy
{
    
    //前去支付页面
    CEMCRXJLCFListPreparePayControllerJuykET *vc = [[CEMCRXJLCFListPreparePayControllerJuykET alloc]init];
    vc.CUWORhdetailModelIRREMd = self.CUWORhdetailModelIRREMd;
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
    
    
    
    
}

- (void)addNavigationBar
{
    [self.navigationBar removeFromSuperview];
    [super addNavigationBar];
    self.navigationBar.backgroundColor = [UIColor clearColor];
    self.navigationBar.title = self.bxiKZJproTitlehvcALF;
    self.navigationBar.titleLabel.textColor = QTZ_White;
    self.navigationBar.titleLabel.font = FONT(15);
    [self.navigationBar setBottomBorderColor:[UIColor clearColor]];
    UIButton *backButton = [UIButton whiteBackBtnWithTarget:self action:@selector(clickback) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitleColor:QTZ_White forState:0];
    [self.navigationBar setLeftBarButton:backButton];
}

- (void)loadData
{
    
    [XvmSEfJLCFListRequestdkZIyb postListDetailDataWithParams:@{@"id": self.proId} loadingInView:self.view success:^(QddEjIJLCFListDetailResultModelNVtUPx *listDetail) {
        self.CUWORhdetailModelIRREMd = listDetail;
        [self configHeaderView];
        [self.tableView reloadData];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

- (void)configHeaderView
{
    
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 230 + IPHONEX_SPACE)];
    rRHNlxheaderViewfJQeRl.backgroundColor = Theme_Color;
    self.tableView.tableHeaderView = rRHNlxheaderViewfJQeRl;
    
    UILabel *DqlqILaprLabelIPueGU = [UILabel new];
    DqlqILaprLabelIPueGU.textColor = QTZ_White;
    DqlqILaprLabelIPueGU.font = FONT(32);
    
    if ([self.CUWORhdetailModelIRREMd.data.addApr isEqualToString:@"0"]) {
        DqlqILaprLabelIPueGU.text = [NSString stringWithFormat:@"%@%%", self.CUWORhdetailModelIRREMd.data.realApr];
    } else {
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%+%@%%", self.CUWORhdetailModelIRREMd.data.realApr, self.CUWORhdetailModelIRREMd.data.addApr]];
        [attributeStr addAttribute:NSFontAttributeName value:FONT(17) range:NSMakeRange(0, attributeStr.length)];
        [attributeStr addAttribute:NSFontAttributeName value:FONT(32) range:NSMakeRange(0, self.CUWORhdetailModelIRREMd.data.realApr.length)];
        DqlqILaprLabelIPueGU.attributedText = attributeStr;
    }
    
    [DqlqILaprLabelIPueGU sizeToFit];
    
    DqlqILaprLabelIPueGU.centerX = FULL_WIDTH / 2;
    DqlqILaprLabelIPueGU.top = 86 + IPHONEX_SPACE;
    [rRHNlxheaderViewfJQeRl addSubview:DqlqILaprLabelIPueGU];
    
    UILabel *tdWNHoaprTitleLabelzSRvts = [UILabel labelWithText:@"期望年化利率" textColor:QTZ_White font:FONT(10)];
    tdWNHoaprTitleLabelzSRvts.centerX = DqlqILaprLabelIPueGU.centerX;
    tdWNHoaprTitleLabelzSRvts.top = DqlqILaprLabelIPueGU.bottom + 2;
    [rRHNlxheaderViewfJQeRl addSubview:tdWNHoaprTitleLabelzSRvts];
    
    
    UILabel *BPFLwEdayLabelHMABqA = [UILabel new];
    BPFLwEdayLabelHMABqA.textColor = QTZ_White;
    BPFLwEdayLabelHMABqA.font = FONT(20);
    BPFLwEdayLabelHMABqA.text = self.CUWORhdetailModelIRREMd.data.timeLimitDay;
    [BPFLwEdayLabelHMABqA sizeToFit];
    BPFLwEdayLabelHMABqA.centerX = FULL_WIDTH / 6 + 10;
    BPFLwEdayLabelHMABqA.top = tdWNHoaprTitleLabelzSRvts.bottom + 35;
    [rRHNlxheaderViewfJQeRl addSubview:BPFLwEdayLabelHMABqA];
    
    UILabel *mzesIYdayTitleLabeliruIgN = [UILabel labelWithText:@"项目期限（天）" textColor:QTZ_White font:FONT(10)];
    mzesIYdayTitleLabeliruIgN.centerX = BPFLwEdayLabelHMABqA.centerX;
    mzesIYdayTitleLabeliruIgN.top = BPFLwEdayLabelHMABqA.bottom + 2;
    [rRHNlxheaderViewfJQeRl addSubview:mzesIYdayTitleLabeliruIgN];
    
    NSString *interestStr = [NSString stringWithFormat:@"%.2f", 10000 * [self.CUWORhdetailModelIRREMd.data.apr floatValue] * [self.CUWORhdetailModelIRREMd.data.timeLimitDay integerValue] / 365 / 100];
    UILabel *hXmEPAintezCFozurestLabelGrseuqnMguKw = [UILabel labelWithText:interestStr textColor:QTZ_White font:FONT(20)];
    hXmEPAintezCFozurestLabelGrseuqnMguKw.centerX = FULL_WIDTH / 2;
    hXmEPAintezCFozurestLabelGrseuqnMguKw.top = BPFLwEdayLabelHMABqA.top;
    [rRHNlxheaderViewfJQeRl addSubview:hXmEPAintezCFozurestLabelGrseuqnMguKw];
    
    UILabel *llMtYainterestTitleLabelidUKEP = [UILabel labelWithText:@"每万元收益(元)" textColor:QTZ_White font:FONT(10)];
    llMtYainterestTitleLabelidUKEP.centerX = hXmEPAintezCFozurestLabelGrseuqnMguKw.centerX;
    llMtYainterestTitleLabelidUKEP.top = hXmEPAintezCFozurestLabelGrseuqnMguKw.bottom + 2;
    [rRHNlxheaderViewfJQeRl addSubview:llMtYainterestTitleLabelidUKEP];
    
    UILabel *lowestAccountLabel = [UILabel new];
    lowestAccountLabel.textColor = QTZ_White;
    lowestAccountLabel.font = FONT(20);
    lowestAccountLabel.text = self.CUWORhdetailModelIRREMd.data.lowestAccount;
    [lowestAccountLabel sizeToFit];
    lowestAccountLabel.centerX = FULL_WIDTH / 6 * 5 + 10;
    lowestAccountLabel.top = BPFLwEdayLabelHMABqA.top;
    [rRHNlxheaderViewfJQeRl addSubview:lowestAccountLabel];
    
    UILabel *lowestAccountTitleLabel = [UILabel labelWithText:@"起投金额(元)" textColor:QTZ_White font:FONT(10)];
    lowestAccountTitleLabel.centerX = lowestAccountLabel.centerX;
    lowestAccountTitleLabel.top = lowestAccountLabel.bottom + 2;
    [rRHNlxheaderViewfJQeRl addSubview:lowestAccountTitleLabel];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 200;
    } else {
        return 31;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        xvEfznJLCFListDetailBmapCellEkyNlk *cell = [xvEfznJLCFListDetailBmapCellEkyNlk dequeueReusableCellForTableView:tableView];
        cell.VWtWqQdataModelcUgEkM = self.CUWORhdetailModelIRREMd.data;
        [cell reloadData];
        return cell;
    }
    xXxnSVJLCFListDetailCellDMjeNR *cell = [xXxnSVJLCFListDetailCellDMjeNR dequeueReusableCellForTableView:tableView];
    [cell setWithIndexPath:indexPath hideRecord:NO];
    
    [cell reloadData];
    return cell;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 10)];
    rRHNlxheaderViewfJQeRl.backgroundColor = [UIColor clearColor];
    return rRHNlxheaderViewfJQeRl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([cell respondsToSelector:@selector(tintColor)]) {
        
        if (tableView == self.tableView) {
            
            CGFloat cornerRadius = 5.f;
            
            cell.backgroundColor = UIColor.clearColor;
            
            CAShapeLayer *layer = [[CAShapeLayer alloc] init];
            
            CGMutablePathRef pathRef = CGPathCreateMutable();
            
            CGRect bounds = CGRectInset(cell.bounds, 5, 0);
            
            if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
                
            } else if (indexPath.row == 0) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
                
            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
                
            } else {
                
                CGPathAddRect(pathRef, nil, bounds);
                
            }
            
            layer.path = pathRef;
            
            CFRelease(pathRef);
            
            layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.f].CGColor;
            
            
            UIView *testView = [[UIView alloc] initWithFrame:bounds];
            
            [testView.layer insertSublayer:layer atIndex:0];
            
            testView.backgroundColor = UIColor.clearColor;
            
            cell.backgroundView = testView;
            
        }
        
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1 ) {
        
        if (indexPath.row == 0) {
            IgAVWgJLCFListDetailWebViewControllerOVuLRc *webVC = [[IgAVWgJLCFListDetailWebViewControllerOVuLRc alloc] init];
            webVC.yNEhJGdetailIdEDqPDD = [self.CUWORhdetailModelIRREMd.data id];
            webVC.url = QTZ_WEB_URL(@"project_detail.html");
            
            [self.navigationController pushViewController:webVC animated:YES];
        }else if (indexPath.row == 1){
            fTtJkxJLCFListRecordViewControllerlJnAft *vc = [[fTtJkxJLCFListRecordViewControllerlJnAft alloc]init];
            vc.proId = self.proId;
            [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
        }else if (indexPath.row == 2){
            IgAVWgJLCFListDetailWebViewControllerOVuLRc *webVC = [[IgAVWgJLCFListDetailWebViewControllerOVuLRc alloc] init];
            webVC.yNEhJGdetailIdEDqPDD = [self.CUWORhdetailModelIRREMd.data id];
            webVC.url = QTZ_WEB_URL(@"notification.html");
            
            [self.navigationController pushViewController:webVC animated:YES];
        }
    }
    return;
}

@end
